package com.chenshu.mobile_test.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.util.HashMap;

/**
 * Created with IntelliJ IDEA
 * Description
 * User：chenshu
 * Date：2024-05-08
 * Time：11:52
 */
@Service
public class MobileServiceImpl implements MobileService{
    private static final String url = "https://v.api.aa1.cn/api/phone/guishu-api.php";
    private final RestTemplate restTemplate = new RestTemplate();

    public HashMap<String, String> getBelonging(String phone) {
        //获取到接口返回的响应
        String request = url + "?phone=" + phone;
        ResponseEntity<String> responseEntity = restTemplate.getForEntity(request, String.class);
        String responseBody = responseEntity.getBody();
        //反序列化为一个map
        ObjectMapper objectMapper = new ObjectMapper();
        HashMap<String, Object> resultMap = null;
        try {
            resultMap = objectMapper.readValue(responseBody, new TypeReference<HashMap<String, Object>>() {});
        } catch (JsonProcessingException e) {
            throw new RuntimeException(e);
        }
        //获取接口响应中的data
        return (HashMap<String, String>) resultMap.get("data");
    }
}
